﻿@using System.Globalization;
@using Smartstore.Core.Content.Media;
@using Smartstore.Utilities;
@using Smartstore.Web.Models.Catalog;

@model ProductSummaryModel
@inject MediaSettings MediaSettings

@{
    var id = "artlist-" + CommonHelper.GenerateRandomDigitCode(10);

    // TODO: (mc) Fetch this from the (new) parent model or the product category
    var thumbAspectRatio = MediaSettings.DefaultThumbnailAspectRatio;
    var imgRatio = string.Empty;

    if (Model.ViewMode != ProductSummaryViewMode.List && thumbAspectRatio != 1 && thumbAspectRatio > 0.2 && thumbAspectRatio < 2)
    {
        imgRatio = thumbAspectRatio.ToString(CultureInfo.InvariantCulture);
    }

    var hasPager = (Model.AvailablePageSizes != null && Model.AvailablePageSizes.Count() > 1) || Model.PagedList.TotalPages > 1;

    // TODO: (mc) AllowFiltering (?)
    var hasActions = Model.PagedList.TotalPages > 1
        || Model.AllowSorting
        || Model.AllowViewModeChanging
        || Model.AllowFiltering;

    var listCssClasses = new List<string> { "artlist" };
    if (Model.ViewMode == ProductSummaryViewMode.List)
    {
        listCssClasses.Add("artlist-lines");
    }
    else
    {
        listCssClasses.Add("artlist-grid");

        if (Model.BoxedStyleItems)
        {
            listCssClasses.Add("artlist-boxed");
        }

        // TODO: (mh) make 6-cols layout fit better per CSS (hide some elements etc.)
        var colspan = (int)Model.GridColumnSpan;
        listCssClasses.Add("artlist-{0}-cols".FormatInvariant(colspan));
    }
}

@if (Model.ViewMode == ProductSummaryViewMode.List)
{
    var maxPictureSize = Model.ThumbSize ?? Model.Items.Select(x => x.Image)
        .Where(x => x != null)
        .Max(x => x.ThumbSize);

    if (maxPictureSize.HasValue)
    {
        <style type="text/css">
		#@id .art-picture-block {
			width: @("{0}px".FormatInvariant(maxPictureSize.Value));
		}
        </style>
    }
}

@if (Model.PagedList.TotalCount > 0 && hasActions)
{
    <div class="artlist-actions artlist-actions--top d-flex flex-wrap" attr-style='(imgRatio.HasValue(), "--img-aspect-ratio: " + imgRatio)'>
        <div sm-if="Model.AllowSorting || Model.AllowFiltering" class="artlist-action-group artlist-action-group--filtersort">
            @{ await Html.RenderPartialAsync("Product.List.FilterSort", Model); }
        </div>
        <div sm-if="hasPager || Model.AllowViewModeChanging" class="artlist-action-group artlist-action-group--page">
            @* ViewMode *@
            @if (Model.AllowViewModeChanging && Model.ViewMode == ProductSummaryViewMode.Grid || Model.ViewMode == ProductSummaryViewMode.List)
            {
                await Html.RenderPartialAsync("Product.List.ViewMode", Model);
            }

            @* Pager *@
            @if (hasPager)
            {
                await Html.RenderPartialAsync("Product.List.Pager", Model);
            }
        </div>
    </div>
}

@if (Model.AllowFiltering)
{
    <partial name="Search.ActiveFilters" model="Model.SourceResult" />
}

<div sm-if="Model.PagedList.TotalCount > 0" id="@id" class='@string.Join(" ", listCssClasses)'>
    @foreach (var product in Model.Items)
    {
        await Html.RenderPartialAsync("Product.List.Item", product);
    }
</div>

<div sm-if="Model.PagedList.TotalPages > 1 && hasPager" class="artlist-actions artlist-actions--bottom">
    <div class="artlist-action-group artlist-action-group--page">
        @{ await Html.RenderPartialAsync("Product.List.Pager", Model); }
    </div>
</div>

